/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.token.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opgea.token.service.DestinationService;
import com.opgea.token.service.TokenDetailsService;
import com.opgea.token.service.TokenService;
import com.opgea.token.util.DateUtil;
import com.opgea.token.util.FacesUtil;
import com.opgea.token.web.dto.DestinationDTO;
import com.opgea.token.web.dto.TokenDetailsDTO;

/**
 * 
 * @author Ramesh
 */
@Component
@Scope("request")
@ManagedBean(name = "receptionController")
@RequestScoped
public class ReceptionController {

    @Autowired
    private TokenDetailsService tokenDetailsService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private DestinationService destinationService;

    private TokenDetailsDTO tokenDetailsDTO = new TokenDetailsDTO();
    private List<TokenDetailsDTO> tokenList = new ArrayList<TokenDetailsDTO>();
    private List<DestinationDTO> destinationList = new ArrayList<DestinationDTO>();
    private String searchKey;
    private Date appointmentDate;

    @PostConstruct
    public void init() {
	Long companyId = FacesUtil.getSessionUserDataObject().getCompanyId();
	destinationList = destinationService.findAllByCompany(companyId);
	if (tokenDetailsDTO.getAppointmentDate() != null) {
	    tokenList = tokenDetailsService.searchTokens(companyId, searchKey,
		    tokenDetailsDTO.getAppointmentDate());
	}
    }

    public TokenDetailsDTO getTokenDetailsDTO() {
	return tokenDetailsDTO;
    }

    public void setTokenDetailsDTO(TokenDetailsDTO tokenDetailsDTO) {
	this.tokenDetailsDTO = tokenDetailsDTO;
    }

    public List<TokenDetailsDTO> getTokenList() {
	return tokenList;
    }

    public void setTokenList(List<TokenDetailsDTO> tokenList) {
	this.tokenList = tokenList;
    }

    public String getSearchKey() {
	return searchKey;
    }

    public void setSearchKey(String searchKey) {
	this.searchKey = searchKey;
    }

    public Date getAppointmentDate() {
	return appointmentDate;
    }

    public void setAppointmentDate(Date appointmentDate) {
	this.appointmentDate = appointmentDate;
    }

    public List<DestinationDTO> getDestinationList() {
	return destinationList;
    }

    public void setDestinationList(List<DestinationDTO> destinationList) {
	this.destinationList = destinationList;
    }

    public String save() {
	Long companyId = FacesUtil.getSessionUserDataObject().getCompanyId();
	tokenDetailsDTO.setCompanyId(companyId);
	tokenDetailsDTO.setAppointmentDate(DateUtil.getYYYYMMDDFromDate(
		appointmentDate, "-"));
	tokenDetailsService.update(tokenDetailsDTO);
	tokenList = tokenDetailsService.searchTokens(companyId, searchKey,
		tokenDetailsDTO.getAppointmentDate());
	return "new.xhtml";
    }

    public String search() {
	Long companyId = FacesUtil.getSessionUserDataObject().getCompanyId();
	tokenList = tokenDetailsService.searchTokens(companyId, searchKey,
		DateUtil.getYYYYMMDDFromDate(appointmentDate, "-"));
	return "new.xhtml";
    }

}
